﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TaskFrame
{
    /// <summary>
    /// 任务
    /// </summary>
    public class WatcherTask
    {
        public WatcherTask()
        {
            Name = GetType().Name;
        }

        /// <summary>
        /// 任务Id
        /// </summary>
        public virtual long Id { get; set; }

        /// <summary>
        /// 任务名称
        /// </summary>
        public virtual string Name { get; set; }

        /// <summary>
        /// 运行间隔
        /// </summary>
        public virtual long Interval { get; set; }

        /// <summary>
        /// 下次运行时间
        /// </summary>
        public virtual long NextRunTime { get; protected set; } = TaskManager.TimeNow;

        /// <summary>
        /// 是否继续
        /// </summary>
        public virtual bool IsContinue { get; protected set; } = true;

        /// <summary>
        /// 持续次数
        /// </summary>
        public virtual int? ContinuedCount { get; set; }

        /// <summary>
        /// 运行前摇
        /// </summary>
        public virtual void PreRun() { IsContinue = ContinuedCount.HasValue ? (--ContinuedCount) > 0 : true; }

        /// <summary>
        /// 运行任务
        /// </summary>
        public virtual void Run() { }

        /// <summary>
        /// 运行后摇
        /// </summary>
        public virtual void PostRun() { NextRunTime += Interval; }
    }
}
